package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.vo.ProjectCardVO;
import com.kaizeli.website.pojo.vo.ProjectBasicInfoVO;
import com.kaizeli.website.pojo.dto.ProjectAcceptanceDTO;
import com.kaizeli.website.pojo.dto.AcceptanceMemberDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.ProjectCardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目卡片管理控制器
 * 用于项目立项和项目结算页面的项目卡片列表
 */
@Slf4j
@RestController
@RequestMapping("/api/project-cards")
@RequiredArgsConstructor
@Tag(name = "项目卡片管理", description = "项目卡片管理相关接口，用于项目立项和项目结算")
@SecurityRequirement(name = "Bearer Authentication")
public class ProjectCardController {
    
    private final ProjectCardService projectCardService;
    
    @Operation(summary = "获取项目卡片列表", description = "获取按项目编号分组的项目卡片列表，包含验收单统计信息")
    @GetMapping
    public Result<List<ProjectCardVO>> getProjectCards() {
        log.info("获取项目卡片列表");
        try {
            List<ProjectCardVO> result = projectCardService.getProjectCards();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取项目卡片列表失败", e);
            return Result.error(500, "获取项目卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据状态获取项目卡片列表", description = "根据项目状态获取项目卡片列表")
    @GetMapping("/status/{status}")
    public Result<List<ProjectCardVO>> getProjectCardsByStatus(
            @Parameter(description = "项目状态", required = true, example = "进行中")
            @PathVariable String status) {
        log.info("根据状态获取项目卡片列表，状态：{}", status);
        try {
            List<ProjectCardVO> result = projectCardService.getProjectCardsByStatus(status);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据状态获取项目卡片列表失败", e);
            return Result.error(500, "根据状态获取项目卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据客户ID获取项目卡片列表", description = "根据客户ID获取该客户的项目卡片列表")
    @GetMapping("/customer/{customerId}")
    public Result<List<ProjectCardVO>> getProjectCardsByCustomer(
            @Parameter(description = "客户ID", required = true, example = "1")
            @PathVariable Long customerId) {
        log.info("根据客户ID获取项目卡片列表，客户ID：{}", customerId);
        try {
            List<ProjectCardVO> result = projectCardService.getProjectCardsByCustomer(customerId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据客户ID获取项目卡片列表失败", e);
            return Result.error(500, "根据客户ID获取项目卡片列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据项目编号获取子项目列表", description = "根据项目编号获取该大项目下的所有子项目")
    @GetMapping("/{projectCode}/subprojects")
    public Result<List<ProjectBasicInfoVO>> getSubProjectsByProjectCode(
            @Parameter(description = "项目编号", required = true, example = "PROJ001")
            @PathVariable String projectCode) {
        log.info("根据项目编号获取子项目列表，项目编号：{}", projectCode);
        try {
            List<ProjectBasicInfoVO> result = projectCardService.getSubProjectsByProjectCode(projectCode);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据项目编号获取子项目列表失败", e);
            return Result.error(500, "根据项目编号获取子项目列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据项目编号获取验收单列表", description = "根据项目编号获取该大项目下的所有验收单")
    @GetMapping("/{projectCode}/acceptances")
    public Result<List<ProjectAcceptanceDTO>> getAcceptancesByProjectCode(
            @Parameter(description = "项目编号", required = true, example = "PROJ001")
            @PathVariable String projectCode) {
        log.info("根据项目编号获取验收单列表，项目编号：{}", projectCode);
        try {
            List<ProjectAcceptanceDTO> result = projectCardService.getAcceptancesByProjectCode(projectCode);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据项目编号获取验收单列表失败", e);
            return Result.error(500, "根据项目编号获取验收单列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据验收单ID获取验收成员列表", description = "根据验收单ID获取该验收单的所有验收成员")
    @GetMapping("/acceptance/{acceptanceId}/members")
    public Result<List<AcceptanceMemberDTO>> getAcceptanceMembers(
            @Parameter(description = "验收单ID", required = true, example = "1")
            @PathVariable Long acceptanceId) {
        log.info("根据验收单ID获取验收成员列表，验收单ID：{}", acceptanceId);
        try {
            List<AcceptanceMemberDTO> result = projectCardService.getAcceptanceMembers(acceptanceId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据验收单ID获取验收成员列表失败", e);
            return Result.error(500, "根据验收单ID获取验收成员列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "根据项目编号获取分包项目列表", description = "根据项目编号获取该大项目下的所有分包项目")
    @GetMapping("/{projectCode}/subcontract-projects")
    public Result<List<ProjectBasicInfoVO>> getSubcontractProjectsByProjectCode(
            @Parameter(description = "项目编号", required = true, example = "PROJ001")
            @PathVariable String projectCode) {
        log.info("根据项目编号获取分包项目列表，项目编号：{}", projectCode);
        try {
            List<ProjectBasicInfoVO> result = projectCardService.getSubcontractProjectsByProjectCode(projectCode);
            return Result.success(result);
        } catch (Exception e) {
            log.error("根据项目编号获取分包项目列表失败", e);
            return Result.error(500, "根据项目编号获取分包项目列表失败：" + e.getMessage());
        }
    }
}